<?php

namespace App\Providers;

use App\Contracts\EventPusher;
use App\Models\Dog;
use App\Models\Notice;
use App\Models\People;
use App\Repositories\CaptchaRepository;
use App\Repositories\KeyHouseRepository;
use App\Repositories\SendSmsRepository;
use App\Repositories\UserRepository;
use App\Rules\CaptchaKey;
use App\Services\DecoratedService;
use App\Services\RedisEventPusher;
use App\Services\Service;
use DB;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider;
use Illuminate\Queue\Events\JobFailed;
use Illuminate\Queue\Events\JobProcessing;
use Illuminate\Queue\Events\JobProcessed;

class AppServiceProvider extends ServiceProvider
{
//    public $bindings = [
//        EventPusher::class => RedisEventPusher::class,
//    ];
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //自定义验证码规则
//        Validator::extend('captcha_key', function($attribute, $value, $parameters){
//            $captchaData = \Cache::get($value);
//            if (!$captchaData) {
//                return false;
//            }
//            return true;
//        });
        Validator::extend('captcha', function($attribute, $value, $parameters){
            $captchaData = \Cache::get(request('captcha_key'));
            if (!$captchaData) {
                return false;
            }
            if (!hash_equals($captchaData['code'], $value)) {
                // 验证错误就清除缓存
                \Cache::forget(request('captcha_key'));
                return false;
            }
            return true;
        });
//        Validator::extend('phone_key', function($attribute, $value, $parameters){
//            $phoneData = \Cache::get($value);
//            if (!$phoneData) {
//                return false;
//            }
//            return true;
//
//        });
        Validator::extend('phone', function($attribute, $value, $parameters){
            $phoneData = \Cache::get(request('phone_key'));
            if (!$phoneData) {
                return false;
            }
            if (!hash_equals($phoneData['code'], $value)) {
                return false;
            }
            return true;
        });
//        Queue::after(function (JobProcessed $event) {
//            Notice::where('id',31)->update(['content'=>31]);
//
//            // $event->connectionName
//            // $event->job
//            // $event->job->payload()
//        });
//
//        Queue::before(function (JobProcessing $event) {
//            Notice::where('id',30)->update(['content'=>30]);
//
//            // $event->connectionName
//            // $event->job
//            // $event->job->payload()
//        });
//        Queue::looping(function () {
//            while (DB::transactionLevel() > 0) {
//                DB::rollBack();
//            }
//        });




//        Queue::failing(function (JobFailed $event) {
//            Notice::where('id',200)->update(['content'=>400]);
//            // $event->connectionName
//            // $event->job
//            // $event->exception
//        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
//        $this->app->resolving(function ($object, $app) {
//            // Called when container resolves object of any type...
//            \Log::info($object.'===='.$app);
//        });
        // 标记
        $this->app->bind('SpeedReport', function () {
            return new SendSmsRepository();
        });

        $this->app->bind('MemoryReport', function () {
            return new CaptchaRepository();
        });

        $this->app->bind('ReportAggregator', function ($app) {
            return new UserRepository($app->tagged('reports'));
        });

        $this->app->tag(['SpeedReport', 'MemoryReport'], 'reports');

        // 这个绑定是  第一个是抽象类，接口可不行 ，第二个就是完成第一个的具体实现
        $this->app->bind(
            'App\Contracts\EventPusher',
            'App\Services\RedisEventPusher'
        );
        // 把 $variableName = 'chen'  进去的时候传 App\Http\Controllers\UserController
        $this->app->when('App\Http\Controllers\UserController')
            ->needs('$variableName')
            ->give('chen');

//        $api = new People(new Dog());
//
//        $this->app->instance('People', $api);


//        $this->app->extend(EventPusher::class,function ($service) {
//            return new RedisEventPusher($service);
//        });
        $this->app->bind(People::class,function ($app) {
            return new People(new Dog());
        });
//        $this->app->singleton(KeyHouseRepository::class,function ($app) {
//            return new KeyHouseRepository();
//        });
//        if (app()->isLocal()) {
//            $this->app->register(\VIACreative\SudoSu\ServiceProvider::class);
//        }
//
//        \API::error(function (\Illuminate\Database\Eloquent\ModelNotFoundException $exception) {
//            abort(404);
//        });
//        \API::error(function (\Illuminate\Auth\Access\AuthorizationException $exception) {
//            abort(403, $exception->getMessage());
//        });
    }
}
